﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using SAD_SiS.Models;

namespace SAD_SiS.Controllers
{
    public class ShopProfileController : Controller
    {
        //
        // GET: /ShopProfile/

        public ActionResult Shop()
        {
            return View();
        }
        public ActionResult ProfileShop()
        {
            return View();
        }

        public ActionResult ChangeProfileShop()
        {
            int shopID = 1;
            using (SADEntities dbContext = new SADEntities())
            {
                var shopInfo = (from shopProfile in dbContext.Shops where shopProfile.ShopID == shopID select shopProfile).Single();
                return View(shopInfo);
            }

        }

        [HttpPost]
        public ActionResult ChangeProfileShop(FormCollection form)
        {
            int shopID = 1;
            string slogan = form["txtSlogan"];
            string address = form["txtAddress"];
            string phone = form["txtPhone"];

            using (SADEntities dbContext = new SADEntities())
            {
                var shop = (from shopProfile in dbContext.Shops where shopProfile.ShopID == shopID select shopProfile).Single();
                // sua tren vung nho
                shop.Slogan = slogan;
                shop.Address = address;
                shop.Phone = phone;
                // sua tren db
                dbContext.SaveChanges();
            }
            return RedirectToAction("Shop");
        }

        public ActionResult ChangeShopLogo()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ChangeShopLogo(HttpPostedFileBase file)
        {
            int shopID = 1;
            var logo = "";
            if (file != null)
            {
                logo = Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath("~/Content/images/"), logo);
                file.SaveAs(path);
            }


            using (SADEntities dbContext = new SADEntities())
            {
                var l = (from shopLogo in dbContext.Shops where shopLogo.ShopID == shopID select shopLogo).Single();
                if (logo != "")
                {
                    l.Logo = logo;
                }
                dbContext.SaveChanges();

            }
            return RedirectToAction("Shop");

        }

    }
}
